package com.code.ddbond.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.code.ddbond.pojo.ResultRes;
import com.code.ddbond.pojo.VideoInfo;
import com.code.ddbond.service.UserDetailService;
import com.code.ddbond.service.VideoInfoService;
import com.code.ddbond.vo.VideoVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpSession;
import org.mybatis.logging.Logger;
import org.mybatis.logging.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MaxUploadSizeExceededException;

@Tag(name = "视频管理")
@Controller
@RequestMapping("/video")
public class VideoInfoController
{

    @Autowired
    private VideoInfoService videoInfoService;

    @Autowired
    private UserDetailService userDetailService;

    @GetMapping("/upload")
    public String showuploadpage()
    {
        return "uploadfile";
    }

    @GetMapping("/mainpage")
    public String showmainpage()
    {
        return "mainpage";
    }


    @Operation(summary = "上传视频")
    @PostMapping("/upload")
    public String uploadVideo(VideoVo videoVo, Model model, HttpSession session) {
        try {
            // 获取当前登录用户的 userId
            Integer userId = (Integer) session.getAttribute("userId");
            if (userId == null)
            {
                model.addAttribute("information", "用户未登录");
                return "uploadfile"; // 返回上传页面
            }

            // 上传视频
            videoInfoService.uploadVideo(videoVo);

            // 增加投稿数量
            userDetailService.increaseContributeNums(userId);

            model.addAttribute("information", "上传成功，投稿数量已增加。");
        } catch (MaxUploadSizeExceededException e) {
            model.addAttribute("information", "文件过大，无法上传。");
            return "uploadfile";
        } catch (Exception e) {
            e.printStackTrace();
            model.addAttribute("information", "上传失败。");
        }
        return "uploadfile"; // 返回上传页面
    }


    @GetMapping("/search")
    public String showsearchpage(Model model)
    {
        model.addAttribute("keyword", ""); // 默认关键字为空
        model.addAttribute("videoPage", new Page<>()); // 添加一个空的 Page 对象
        return "videosearch";
    }

    @Operation(summary = "搜索视屏")
    @PostMapping("/search")
    public String searchVideos(@RequestParam("keyword") String keyword,
                               @RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
                               @RequestParam(value = "pageSize", defaultValue = "1") int pageSize,
                               Model model)
    {
        Page<VideoInfo> videoPage = videoInfoService.searchVideos(keyword, pageNum, pageSize);
        model.addAttribute("videoPage", videoPage);
        model.addAttribute("keyword", keyword);
        return "videolist";
    }

}
